package org.example.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.example.mapper.EmpExprMapper;
import org.example.mapper.EmpMapper;
import org.example.pojo.*;
import org.example.service.EmpService;
import org.example.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmpServiceImpl implements EmpService{
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;
//    @Override
//    public PageResult<Emp> page( Integer page, Integer pageSize){
//        List<Emp> list = empMapper.list((page - 1) * pageSize, pageSize);
//        Long count = empMapper.count();
//        PageResult<Emp> pageResult = new PageResult<>(count, list);
//        return pageResult;
//    }


    @Override
    public LoginInfo login(Emp emp){
       Emp emp1 = empMapper.login(emp);
       if(emp1 != null){
           // 生成令牌
           Map<String,Object> dataMap = new HashMap<>();
           dataMap.put("id", emp1.getId());
           dataMap.put("username", emp1.getUsername());

           String jwt = JwtUtils.generateJwt(dataMap);
           LoginInfo loginInfo = new LoginInfo(emp1.getId(),emp1.getUsername(),emp1.getName(),jwt);
           return loginInfo;
       }
        return null;
    }

    @Override
    public List<Emp> list(){
        List<Emp> list = empMapper.list(null);
        return list;
    }

    //通过分页查询插件
    @Override
    public PageResult<Emp> page( EmpQueryParam param){
        PageHelper.startPage(param.getPage(), param.getPageSize());
        List<Emp> list = empMapper.list(param);
        Page<Emp> page1 = (Page<Emp>) list;
        return new PageResult<>(page1.getTotal(),page1.getResult());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(Emp emp){
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);

        //删除expr表数据
        empExprMapper.deleteByEmpIds(Collections.singletonList(emp.getId()));
        //判断exprList是否为空
        if(!CollectionUtils.isEmpty(emp.getExprList())){
            //插入id
            emp.getExprList().forEach(expr -> {
                expr.setEmpId(emp.getId());
            });
            empExprMapper.insertExpr(emp.getExprList());
        }

    }

    @Override
    public Emp getById(Integer id){
        Emp emp = empMapper.getById(id);
        return emp;
    }

    //批量删除
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteByIds(List<Integer> ids){
        empMapper.deleteByIds(ids);
        empExprMapper.deleteByEmpIds(ids);

    }

    //事务管理
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(Emp emp){
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp);


        List<EmpExpr> exprList = emp.getExprList();
        if(!CollectionUtils.isEmpty(exprList)){
            for (EmpExpr expr : emp.getExprList()) {
                expr.setEmpId(emp.getId());
            }
            empExprMapper.insertExpr(emp.getExprList());
        }

    }
}
